<?php

class Domain_App_Apply {
    function __construct() {
        $this->_model = new Model_App_Apply();
    }

    /**
     * 添加一条用户信息
     * @param $data
     * @return mixed
     */
    public function addApply($data) {
        // 校验数据
        if (!DI()->helper->validateMobileNo($data['contact'])) {
            DI()->logger->error("电话号码格式有误");
            throw new PhalApi_Exception('MOBILE NO FORMAT EXCEPTION', 400);
        }
        if (!DI()->helper->validateUsername($data['uname'])) {
            DI()->logger->error("姓名格式填写有误");
            throw new PhalApi_Exception('NAME FORMAT EXCEPTION', 400);
        }
        // 检查提交的数据是否重复
        $checkDuplicate = $this->_model->checkApplyDuplicate($data);
        if ($checkDuplicate) throw new PhalApi_Exception('DUPLICATE', 400);

        //检查项目是否存在
//        $checkProject = $this->_model->getProjectDataById($data['pid']);
//        if (!$checkProject) throw new PhalApi_Exception('PROJECT NOT FOUND', 400);

        $modelRes = $this->_model->insertUser($data);
        if (!$modelRes) {
            DI()->logger->error("插入用户数据失败");
            throw new PhalApi_Exception('INSERT FAIL', 400);
        }
        return $modelRes;
    }
    /**
     *根据 id  删除项目表中的一条数据
     *
     */
    public function deleteApplyByOpenid($apply_id){
        return $this->_model->deleteApplyByOpenid($apply_id);
    }
    /**
     * 根据 apply_id 获取 相应数据
     *
     */
    public function getApplyByApplyId($apply_id){
        return $this->_model->getApplyByApplyId($apply_id);
    }
    /**
     * 获取 客户资料列表
     */
    public function getApplyList(){
        return $this->_model->getApplyList();
    }
    public function edit($data,$apply_id){
        if (!DI()->helper->validateMobileNo($data['contact'])) {
            DI()->logger->error("电话号码格式有误");
            throw new PhalApi_Exception('MOBILE NO FORMAT EXCEPTION', 400);
        }
        if (!DI()->helper->validateUsername($data['uname'])) {
            DI()->logger->error("姓名格式填写有误");
            throw new PhalApi_Exception('NAME FORMAT EXCEPTION', 400);
        }
        //检查 apply_id 是否存在
        $checkDuplicate = $this->_model->checkApplyDuplicate($data);
        if (!$checkDuplicate) throw new PhalApi_Exception('APPLY_ID NOT FOUND', 400);
        return $this->_model->edit($data,$apply_id);
    }
}

